package com.cuitrip.util.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * Created by MiJack on 2015/12/4.
 */
public class InputTime {
    private long time;

    public long getTime() {
        return time;
    }

    /**
     * @param timeLong 格林威治时间的long形式的String类型
     * @return
     */
    public static InputTime build(String timeLong) {
        long time = -1;
        try {
            time = Long.valueOf(timeLong);
        } catch (NumberFormatException e) {
        }
        return build(time);
    }

    /**
     * @param time 格林威治时间的long类型
     * @return
     */
    public static InputTime build(long time) {
        InputTime inputTime = new InputTime();
        inputTime.time = time;
        return inputTime;
    }

    public static InputTime buildWithZeroZone(String time, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT +000"));
        try {
            return build(dateFormat.parse(time).getTime());
        } catch (ParseException e) {
            return build(-1l);
        }
    }

    public static InputTime build(String time, TimeZone timeZone, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setTimeZone(timeZone);
        try {
            return build(dateFormat.parse(time).getTime());
        } catch (ParseException e) {
            return build(-1l);
        }
    }

    public static InputTime build(String time, String format) {
        SimpleDateFormat dateFormat=new SimpleDateFormat(format);
        try {
            return build(dateFormat.parse(time).getTime());
        } catch (ParseException e) {
            return build(-1l);
        }
    }

    public static InputTime build(long t, TimeZone timeZone) {
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeZone(timeZone);
        calendar.setTimeInMillis(t);
        return build(calendar.getTime().getTime());
    }

    public static InputTime build(String value, TimeZone timeZone) {
        long time = -1;
        try {
            time = Long.valueOf(value);
        } catch (NumberFormatException e) {
        }
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeZone(timeZone);
        calendar.setTimeInMillis(time);
        return build(calendar.getTime().getTime());
    }

    /**
     * 转换其他时间到服务器计时的timezone
     *
     * @param time
     * @param inputTimeZone
     * @return
     */
    public static long outputOtherZoneTimeMills(long time, TimeZone inputTimeZone, TimeZone outPutTimeZone) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        calendar.setTimeZone(inputTimeZone);

        Calendar outputCalendar = Calendar.getInstance();
        outputCalendar.setTimeZone(outPutTimeZone);
        outputCalendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        outputCalendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        outputCalendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
        outputCalendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
        outputCalendar.set(Calendar.MINUTE, 0);
        outputCalendar.set(Calendar.SECOND, 0);
        outputCalendar.set(Calendar.MILLISECOND, 0);
        return outputCalendar.getTimeInMillis();
    }

}

